﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConjureBivouac2012.GameStates;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace ConjureBivouac2012.Object
{
    public class BasicEnemy : AbstractEnemy
    {
        bool newBulletNeeded = false;
        private float fireDelay;
        private double lastShootFired;

        public BasicEnemy(float fireDelay, double lastShootFired) : base(80, 5)
        {
            this.fireDelay = fireDelay;
            this.lastShootFired = lastShootFired;
        }

        // Return a new bullet that has been shoteed
        public override Entity[] addingEntity()
        {
            Entity[] bullets = new Entity[1];
            if (newBulletNeeded)
            {
                newBulletNeeded = false;
                NormalBullet b = new NormalBullet();

                b.Fire(this, GetFireDirection());
                bullets[0] = b;
                return bullets;
            }

            else return null;
        }

        public override void LoadContent(ContentManager contentManager)
        {
            contentName = "Ennemy/EnnemyA";
            this.sprite.LoadContent(contentManager, contentName);
            base.LoadContent(contentManager);
        }

        public override void Update(GameTime time)
        {
            base.Update(time);
            Fire(time);
        }

        public void Fire(GameTime time)
        {
            if (fireDelay > 0 && (lastShootFired == -1 || time.TotalGameTime.TotalSeconds - lastShootFired >= fireDelay))
            {
                lastShootFired = time.TotalGameTime.TotalSeconds;
                newBulletNeeded = true;

                /**Vector2 dir = GetFireDirection();

                foreach (var bullet in _bullets)
                {
                    //Unused bullet instance
                    if (!bullet.Visible)
                    {
                        newBulletNeeded = false;

                        if (dir.X != 0 || dir.Y != 0)
                        {
                            bullet.Fire(this.sprite.Position, dir);
                        }
                        break;
                    }
                }

                //Creates a new bullet
                if(newBulletNeeded)
                {
                    AbstractBullet bullet = CreateNewBullet();
                    bullet.Initialize();
                    bullet.LoadContent(_contentManager);
                    bullet.Fire(this.sprite.Position, dir);
                    _bullets.Add(bullet);
                }*/
            }
        }

         private Vector2 GetFireDirection()
         {
             Vector2 direction = new Vector2(0, 0);

             IGlobalGameState state = Program.GetGameInstance.GetCurrentState();

             AbstractPlayer p = null;

             if(state is PlayGame)
             {
                 p = ((PlayGame) state).GetPlayer();
             }

             if (p != null)
             {
                 direction = p.GetVehicule().sprite.Position - this.sprite.Position;
                 direction.Normalize();
             }

             return direction;
         }

         //protected abstract AbstractBullet CreateNewBullet();
    }
}
